package com.lanou.utp.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lanou.utp.entity.Clazz;
import com.lanou.utp.utils.IdAndName;
import com.lanou.utp.mapper.ClazzMapper;
import com.lanou.utp.service.ClazzService;
import com.lanou.utp.entity.Paging;
import com.lanou.utp.entity.ResponseData;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * @author MaQingYun
 */
@Transactional
@Service
public class ClazzServiceImpl implements ClazzService {

    @Resource
    private ClazzMapper clazzMapper;

    @Override
    public ResponseData getGrade() {
        ArrayList<Object> getGradeList = new ArrayList<>();
//        List<Clazz> clazzList = this.clazzMapper.getGrade();
//        for (Clazz clazz : clazzList) {
//            IdAndName idAndName = new IdAndName();
//            idAndName.setId(Long.valueOf(clazz.getGrade()));
//            idAndName.setName(clazz.getGrade());
//            getGradeList.add(idAndName);
//        }
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        for (int i = 0; i <4 ; i++) {
            IdAndName idAndName = new IdAndName();
            idAndName.setId((long) (year - i));
            idAndName.setName(String.valueOf(year-i));
            getGradeList.add(idAndName);
        }
        return new ResponseData("ok",getGradeList);
    }

    @Override
    public ResponseData getClassList(Integer page) {
        Page<Object> page1 = PageHelper.offsetPage((page - 1) * Paging.size, Paging.size);
        List<Clazz> clazzList = this.clazzMapper.findClassList();
        Paging paging = new Paging(page,page1.getTotal());
        return new ResponseData(clazzList, paging);
    }

    @Override
    public void addClazz(Clazz clazz) {
       this.clazzMapper.addClazz(clazz);

    }

    @Override
    public void updateClazz(Clazz clazz) {
        this.clazzMapper.putClazz(clazz);
    }

    @Override
    public void batchDeleteClazzes(List list) {
        this.clazzMapper.batchDeleteClazzes(list);
    }


}
